package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.ChronicDiseaseCertificate;
import com.lxw.nonghe.service.ChronicDiseaseCertificateService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 慢病症控制器
 */
@RestController
@RequestMapping("/chronicDiseaseCertificate")
@RequiresPermission("certificate:manage")
public class ChronicDiseaseCertificateController {

    @Autowired
    private ChronicDiseaseCertificateService certificateService;

    @Autowired
    private LogUtils logUtils;

    /**
     * 添加慢病证
     * @param certificate
     * @return
     */
    @PostMapping("/create")
    public Result<Boolean> addCertificate(@RequestBody ChronicDiseaseCertificate certificate) {
        try {
            if (certificate.getStartTime() == null) {
                certificate.setStartTime(new Date());
            }

            boolean result = certificateService.save(certificate);
            if (result) {
                logUtils.recordLog("慢病证管理", "新增慢病证", "成功",
                        "农民ID:" + certificate.getFarmerId() + ", 疾病:" + certificate.getDiseaseName());
                return Result.success("慢病症记录新增成功", true);
            } else {
                logUtils.recordLog("慢病证管理", "新增慢病证", "失败", "保存数据库失败");
                return Result.error("慢病症记录新增失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢病证管理", "新增慢病证", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    /**
     * 删除慢病证
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteCertificate(@PathVariable Integer id) {
        try {
            ChronicDiseaseCertificate certificate = certificateService.getById(id);
            boolean result = certificateService.removeById(id);

            if (result) {
                logUtils.recordLog("慢病证管理", "删除慢病证", "成功",
                        "记录ID:" + id + ", 疾病:" + (certificate != null ? certificate.getDiseaseName() : "未知"));
                return Result.success("慢病症记录删除成功", true);
            } else {
                logUtils.recordLog("慢病证管理", "删除慢病证", "失败", "记录ID:" + id + "不存在");
                return Result.error("慢病症记录删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢病证管理", "删除慢病证", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    /**
     * 修改慢病证
     * @param certificate
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> updateCertificate(@RequestBody ChronicDiseaseCertificate certificate) {
        try {
            boolean result = certificateService.updateById(certificate);
            if (result) {
                logUtils.recordLog("慢病证管理", "更新慢病证", "成功",
                        "记录ID:" + certificate.getCertificateId());
                return Result.success("慢病症记录更新成功", true);
            } else {
                logUtils.recordLog("慢病证管理", "更新慢病证", "失败",
                        "记录ID:" + certificate.getCertificateId() + "不存在");
                return Result.error("慢病症记录更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢病证管理", "更新慢病证", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<ChronicDiseaseCertificate> getCertificateById(@PathVariable Integer id) {
        try {
            ChronicDiseaseCertificate certificate = certificateService.getById(id);
            if (certificate != null) {
                /*logUtils.recordLog("慢病证管理", "查询慢病证详情", "成功", "记录ID:" + id);*/
                return Result.success("慢病症记录查询成功", certificate);
            } else {
                logUtils.recordLog("慢病证管理", "查询慢病证详情", "失败", "记录ID:" + id + "不存在");
                return Result.error("未找到该慢病症记录");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢病证管理", "查询慢病证详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/getByDisease/{diseaseName}")
    public Result<ChronicDiseaseCertificate> getCertificateByDiseaseName(@PathVariable String diseaseName) {
        try {
            ChronicDiseaseCertificate certificate = certificateService.getChronicDiseaseCertificateByDiseaseName(diseaseName);
            if (certificate != null) {
                /*logUtils.recordLog("慢病证管理", "按疾病查询", "成功", "疾病名称:" + diseaseName);*/
                return Result.success("慢病症记录查询成功", certificate);
            } else {
                logUtils.recordLog("慢病证管理", "按疾病查询", "失败", "疾病名称:" + diseaseName + "无记录");
                return Result.error("未找到该疾病的慢病症记录");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢病证管理", "按疾病查询", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<ChronicDiseaseCertificate>> getCertificatePage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String diseaseName) {
        try {
            Page<ChronicDiseaseCertificate> page = certificateService.getChronicDiseaseCertificatePage(current, size, diseaseName);
            /*logUtils.recordLog("慢病证管理", "分页查询", "成功",
                    "页码:" + current + ", 大小:" + size + ", 疾病:" + diseaseName);*/
            return Result.success("慢病症记录分页查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("慢病证管理", "分页查询", "失败", e.getMessage());
            return Result.error("分页查询异常: " + e.getMessage());
        }
    }

    @PostMapping("/query")
    public Result<Page<ChronicDiseaseCertificate>> queryCertificates(
            @RequestBody ChronicDiseaseCertificate queryParam,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<ChronicDiseaseCertificate> page = certificateService.queryChronicDiseaseCertificate(queryParam, current, size);
            /*logUtils.recordLog("慢病证管理", "条件查询", "成功",
                    "条件:" + queryParam.toString() + ", 页码:" + current + ", 大小:" + size);*/
            return Result.success("慢病症记录条件查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("慢病证管理", "条件查询", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/getByFarmer/{farmerId}")
    public Result<Page<ChronicDiseaseCertificate>> getCertificatesByFarmer(
            @PathVariable Integer farmerId,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            ChronicDiseaseCertificate query = new ChronicDiseaseCertificate();
            query.setFarmerId(farmerId);
            Page<ChronicDiseaseCertificate> page = certificateService.queryChronicDiseaseCertificate(query, current, size);
            /*logUtils.recordLog("慢病证管理", "按农民查询", "成功", "农民ID:" + farmerId);*/
            return Result.success("农民慢病症记录查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("慢病证管理", "按农民查询", "失败", e.getMessage());
            return Result.error("农民查询异常: " + e.getMessage());
        }
    }
}
